<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Phone extends CrmController
{
    /**
     * phonePool
     */
    public function phonePool()
    {
        $menuname = "Phone Pool";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);

        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);

        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);

        $member = Cache::get('member');
        $this->assign("member",$member);

        // 模板输出
        return $this->fetch('phonePool');
    }


    /**
     * 渲染模板importCustomer
     */
    public function importCustomer()
    {
        $menuname = "Phone Pool";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // 模板输出
        return $this->fetch('importCustomer');
    }


    /**
     * 渲染模板transferCustomer
     */
    public function transferCustomer()
    {
        $menuname = "Phone Pool";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // 查询当前所有任务
        $taskOption = Loader::model('Phone')->outBoundTask();
        $this->assign('taskOption', $taskOption);

        // 查询当前号码池总量
        $phoneCount = Loader::model('Phone')->phoneCount($_REQUEST['tables']);
        $this->assign('phoneCount', $phoneCount);
        // echo $id;exit;
        $this->assign('import_mark', $_REQUEST['import_mark']);
        $this->assign('tables', $_REQUEST['tables']);
        // 模板输出
        return $this->fetch('transferCustomer');
    }


    /**
     * 转交外呼任务
     */
    public function doTransferCustomer ()
    {
        // print_r($_REQUEST);exit;
        if (empty($_REQUEST['tables'])) {
            echo json_encode(array('msg'=>'操作失败！'));
            exit;
        } else {
            $tables = 'vip_phone_pool_'.$_REQUEST['tables'];
        }

        Loader::model('Phone')->transferCustomer($tables);
        // echo json_encode($ids);
    }


    /**
     * 导入excel
     */
    public function doImportCustomer ()
    {
        // print_r($_REQUEST);exit;
        Loader::model('Phone')->importExcel();
    }


    /**
     * 获取客户资料列表
     * @return [json] [客户资料列表]
     */
    public function phonePoolData()
    {
        // 先判断哪张表
        if (empty($_REQUEST['tables'])) {
            $tables = 'vip_phone_pool_a';
        } else {
            $tables = 'vip_phone_pool_'.$_REQUEST['tables'];
        }
        // $users = Cache::get('users');
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        //需先判断传值是否为空
        // print_r($_REQUEST);exit;
        $where = "(1 ";
        $where .= empty($_REQUEST["name"])?"":" AND name like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["import_mark"])?"":" AND import_mark like '%".$_REQUEST["import_mark"]."%'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND customer_code like '%".$_REQUEST["customer_code"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone like '%".$_REQUEST["phone"]."%'";
        $where .= empty($_REQUEST["start_createtime"])?"":" AND createtime > '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime < '".$_REQUEST["end_createtime"]."'";
        $where .= " )";

        $arrData = Loader::model('Phone')->phonePoolData($where,$page,$limit,$tables);
        echo json_encode($arrData);
    }


    /**
     * 删除客户资料
     * @return [type] [description]
     */
    public function delPhonePool()
    {
        $customer_code = $_REQUEST["customer_code"];
        if (empty($_REQUEST['tables'])) {
            echo json_encode(array('msg'=>'删除失败！'));
            exit;
        } else {
            $tables = 'vip_phone_pool_'.$_REQUEST['tables'];
        }
        Loader::model('Phone')->delPhonePool($customer_code, $tables);
    }


}